package com.xingdata.jjxc.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.LocalVideoEntity;
import com.xingdata.jjxc.utils.BitmapCache;
import com.xingdata.jjxc.utils.BitmapThumbnailTask;
import com.xingdata.jjxc.utils.BitmapThread;
import com.xingdata.jjxc.utils.Consumer;
import com.xingdata.jjxc.utils.ImageTools;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.VideoTools;
import com.xingdata.jjxc.views.MyImageView;

public class LocalVideoAdapter extends BaseAdapter {
	private List<LocalVideoEntity> localvideolist;
	private LocalVideoEntity localvideoentity;
	private LayoutInflater inflater;
	private Context context;
	private LinearLayout localVideoCachess;
	private LruCache<String, Bitmap> bitMap = new LruCache<String, Bitmap>(1*2024*1024);
	private Object synchronizedObject = new Object();
	private String TAG = "LocalVideoAdapter";
	private Handler handler;
	private ArrayList<ImageView> imageViews;
	private ArrayList<String> downLoadURLs = new ArrayList<String>();
	private Consumer consumer;

	public LocalVideoAdapter(FragmentActivity activity,
			List<LocalVideoEntity> mlist, ArrayList<ImageView> imageViews,Consumer consumer) {
		this.context = activity;
		this.localvideolist = mlist;
		this.imageViews = imageViews;
		this.consumer=consumer;
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageCode.MessageCode_RefreshControl:
					notifyDataSetChanged();
					break;

				default:
					break;
				}
			};
		};
	}

	@Override
	public int getCount() {
		return localvideolist == null ? 0 : localvideolist.size();
	}

	@Override
	public LocalVideoEntity getItem(int position) {
		// TODO Auto-generated method stub
		return localvideolist.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder mholder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.localvideofragment, null);
			mholder = new Holder();
			mholder.splendidview = (MyImageView) convertView
					.findViewById(R.id.local_video_image);
			mholder.fSize = (TextView) convertView.findViewById(R.id.fSize);
			mholder.crtTime = (TextView) convertView.findViewById(R.id.crtTime);
			mholder.crtDate = (TextView) convertView.findViewById(R.id.crtDate);
			// mholder.splendidview.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// switch (v.getId()) {
			// case R.id.splendidview:
			//
			// // localVideoCache=new LocalVideoCache(context);
			// // localVideoCache.getClass().getMethods();
			// // Intent intent=new
			// // Intent(context,LocalVideoCache.class);
			// // context.startActivity(intent);
			// break;
			// }
			// }
			// });
			if (!imageViews.contains(mholder.splendidview)) {
				imageViews.add(mholder.splendidview);
			}

			convertView.setTag(mholder);
		} else {
			mholder = (Holder) convertView.getTag();
		}
		localvideoentity = localvideolist.get(position);
		mholder.fSize.setText(localvideoentity.getfSize());
		mholder.crtTime.setText(localvideoentity.getCrtTime());
		mholder.crtDate.setText(localvideoentity.getCrtDate());
		// mholder.circulationdate.setText(localvideoentity.getCirculationdate());
		// mholder.flow.setText(localvideoentity.getFlow());

		String imageURL = ImageTools.changeImageName(localvideoentity.getUrl()
				.replaceAll(".mp4", ".jpg"));

		if (BitmapCache.get(localvideoentity.getUrl()) != null) {
			mholder.splendidview.setImageBitmap(BitmapCache.get(localvideoentity.getUrl()));
		} else {
			if (!downLoadURLs.contains(localvideoentity.getUrl())) {
				// new getBitMapThread(localvideoentity.getUrl()).start();
				downLoadURLs.add(localvideoentity.getUrl());
//				new BitmapThread(localvideoentity.getUrl(), handler,bitMap)
//						.start();
				consumer.add(new BitmapThumbnailTask(localvideoentity.getUrl(), handler,2.0f));
			}

		}

		return convertView;
	}

	class Holder {
		private MyImageView splendidview;
		private TextView fSize, crtTime, crtDate;
	}

//	class getBitMapThread extends Thread {
//		String url;
//
//		public getBitMapThread(String url) {
//			this.url = url;
//			downLoadURLs.add(url);
//		}
//
//		@Override
//		public void run() {
//			synchronized (synchronizedObject) {
//				Log.i(TAG, "正在获取缩略图" + url);
//				String imageURL = url.replaceAll(".mp4", ".jpg");
//				Bitmap bitmap = SDCardTools.getSDImage(imageURL);
//				if (bitmap == null) {
//					bitmap = VideoTools.createVideoThumbnail(url);
//					SDCardTools.SaveUrlImage(imageURL, bitmap);
//				}
//				BitmapCache.put(ImageTools.changeImageName(imageURL), bitmap);
//				Message message = new Message();
//				message.what = MessageCode.MessageCode_RefreshControl;
//				handler.sendMessage(message);
//			}
//			super.run();
//
//		}
//	}

}
